package com.ewzj.bigdata.mvp.presenter

import android.support.v4.app.Fragment
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.utils.RxUtils
import com.ewzj.bigdata.mvp.contract.OrginNewsFragmentContract
import com.ewzj.bigdata.mvp.model.reqbean.Reqpagebean
import com.ewzj.bigdata.mvp.model.rspbean.CityBean
import com.ewzj.bigdata.mvp.model.rspbean.RspOriginBean
import com.ewzj.bigdata.mvp.ui.adapter.OriginAdapter
import com.google.gson.Gson
import com.jess.arms.mvp.BasePresenter
import com.jess.arms.utils.DataHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber
import javax.inject.Inject

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class OrginNewsFragmentPresenter @Inject constructor(val mGson :Gson,val rxErrorHandler :RxErrorHandler,view:OrginNewsFragmentContract.View,modle:OrginNewsFragmentContract.Model):BasePresenter<OrginNewsFragmentContract.Model,OrginNewsFragmentContract.View>(modle,view){
    lateinit var adapter: OriginAdapter
    var newdatas= ArrayList<RspOriginBean>()
    var page:Int=1
    var locationcity: String = ""
    val context=(mRootView as Fragment).context
    fun initRecyadapter(){
        adapter= OriginAdapter(newdatas)
        mRootView.initRecylerview(adapter)
        adapter.disableLoadMoreIfNotFullPage()
    }

    fun getFirstData(){
        val pagebean= Reqpagebean()
        page=1
        mModel.getnewsByPage(pagebean).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object :ErrorHandleSubscriber<List<RspOriginBean>>(rxErrorHandler){
                    override fun onNext(t: List<RspOriginBean>) {
                        page++
                        newdatas.clear()
                        newdatas.addAll(t)
                        adapter.setNewData(newdatas)
                        if (t.size<AppKey.PAGE_NUM){
                            adapter.disableLoadMoreIfNotFullPage()
                        }
                    }

                })
    }
    fun loadmoreData(){
        val pagebean= Reqpagebean(page)
        mModel.getnewsByPage(pagebean).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doAfterTerminate { if (adapter.isLoading){
                    adapter.loadMoreComplete()
                } }
                .subscribe(object :Consumer<List<RspOriginBean>>{
                    override fun accept(t: List<RspOriginBean>) {
                        page++
                        if (t.size<AppKey.PAGE_NUM){
                            adapter.addData(t)
                            adapter.loadMoreEnd()
                        }else{
                            adapter.addData(t)
                        }
                    }
                }, Consumer { adapter.loadMoreFail() })
    }
    fun onreusme(){
            var spcity = DataHelper.getStringSF(context, AppKey.LOCATION_CITY)
            if (spcity.isNullOrEmpty()) return
            if (!locationcity.equals(spcity)) {
                var citybean = mGson.fromJson(spcity, CityBean.ListEntity::class.java)
                getFirstData()
                locationcity = DataHelper.getStringSF(context, AppKey.LOCATION_CITY)
        }
    }
}